package likou.duoxianc;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/10/08 18:11
 */
public class _1115 {
    static class FooBar{
        private int n;
        private volatile boolean b = true;

        public FooBar(int n) {
            this.n = n;
        }

        public void foo(Runnable printFoo) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                if (b){
                    printFoo.run();
                    b=false;
                }else {
                    Thread.yield();
                    i--;
                }
            }
        }

        public void bar(Runnable printBar) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                if (!b){
                    printBar.run();
                    b=true;
                }else {
                    Thread.yield();
                    i--;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        FooBar fooBar = new FooBar(5);
        fooBar.foo(new Thread(()->{
            System.out.print("foo");
        }));
        fooBar.bar(new Thread(()->{
            System.out.print("bar");
        }));
    }
}
